package com.crm.redis.service.cluePoolManager.impl;

import com.crm.model.entity.cluePoolManager.Clue;
import com.crm.model.entity.cluePoolManager.CluePoolPublic;
import com.crm.redis.RedisPrefix;
import com.crm.redis.mapper.ValueRMapper;
import com.crm.redis.service.cluePoolManager.ClueRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author luojie
 * @date 2021/2/5 15:45
 */
@Service
public class ClueRedisServiceImpl implements ClueRedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 根据线索id获取key
     * @param id
     * @return
     */
    private String getKey(String id){
        String key = RedisPrefix.CLUE + id;
        return key;
    }

    /**
     * 根据省市id获取key
     * @param provinceId
     * @return
     */
    private String getClueNumberKey(String provinceId) {
        String key = RedisPrefix.CLUE_NUMBER + provinceId;
        return key;
    }

    /**
     * 根据公司名和社会统一信用代码获取key
     * @param companyName 公司名
     * @param unifiedSocialCreditCode 社会统一信用代码
     * @return
     */
    private String getClueIsRepeatKey(String companyName,String unifiedSocialCreditCode){
        String key = RedisPrefix.CLUE_IS_REPEAT + companyName + unifiedSocialCreditCode;
        return key;
    }

    /***
     * @Description: 根据省id获取线索编号
     * @Param: provinceId 省id
     * @Author: luojie
     * @Date: 2021/5/14 10:54
     */
    @Override
    public String getClueNumberByProvinceId(String provinceId) {
        String key = getClueNumberKey(provinceId);
        Integer clueNumber = ValueRMapper.getRedisInteger(redisTemplate, key);
        //先查询出redis里面有没有使用数量，没有则赋初始值
        if(clueNumber==null){
            clueNumber=1;
        }else {
            setUsageCountById(provinceId,clueNumber+1);
        }
        int len = 4;
        StringBuilder sb = new StringBuilder();
        sb.append(clueNumber+1);
        int temp = len - sb.length();
        if (temp > 0)
            while (sb.length() < len)   //若长度不足进行补零
                sb.insert(0, "0");
        return "" + sb;
    }

    public Boolean setUsageCountById(String provinceId,Integer clueNumber) {
        String key = getClueNumberKey(provinceId);
        boolean result = ValueRMapper.setRedisObject(redisTemplate, key,clueNumber);
        return result;
    }


    @Override
    public Clue getClueById(String clueId) {
        String key = getKey(clueId);
        Clue clue =  ValueRMapper.getRedisObject(redisTemplate,key, Clue.class);
        return clue;
    }

    @Override
    public Boolean setClueById(Clue clue) {
        String id = clue.getClueId();
        String key = getKey(id);
        return ValueRMapper.setRedisObject(redisTemplate,key,clue);
    }

    @Override
    public Boolean deleteClueById(String clueId) {
        String key = getKey(clueId);
        return ValueRMapper.delRedis(redisTemplate,key);
    }

    @Override
    public Clue getClueByCompanyNameAndCode(String companyName, String unifiedSocialCreditCode) {
        String key = getClueIsRepeatKey(companyName,unifiedSocialCreditCode);
        Clue clue =  ValueRMapper.getRedisObject(redisTemplate,key, Clue.class);
        return clue;
    }

    @Override
    public Boolean setClueByCompanyNameAndCode(Clue clue) {
        String key = getClueIsRepeatKey(clue.getCompanyName(),clue.getUnifiedSocialCreditCode());
        return ValueRMapper.setRedisObject(redisTemplate,key,clue);
    }

    @Override
    public Boolean deleteClueByCompanyNameAndCode(String companyName, String unifiedSocialCreditCode) {
        String key = getClueIsRepeatKey(companyName,unifiedSocialCreditCode);
        return ValueRMapper.delRedis(redisTemplate,key);
    }
}
